Skip to content

perf: use vector instead CDeterministicMNList for computing rotating quorums#6678

Merged
PastaPastaPasta merged 7 commits intodashpay:developfrom
knst:perf-using-vector
Jun 2, 2025
Merged

perf: use vector instead CDeterministicMNList for computing rotating quorums#6678
PastaPastaPasta merged 7 commits intodashpay:developfrom
knst:perf-using-vector

Conversation

@knst
Copy link
Collaborator

@knst knst commented May 20, 2025

Issue being fixed or feature implemented

Helper to get quorum members uses CDeterministicMNList as a source of data.
It makes temporary calculation slower, because temporary lists of masternodes should not construct heavy objects CDeterministicMNList just to keep a list of them.

What was done?

  • helpers CalculateQuorum, CalculateScores are moved from class CDeterministicMNList to llmq::utils so far as they don't actually use any private data of CDeterministicMNList
  • helper CalculateQuorum can accept as source of data not only CDeterministicMNList but std::vector with shared ptrs.
  • helper BuildNewQuorumQuarterMembers does not use CDeterministicMNList for MnsNotUsedAtH
  • GetMNUsageBySnapshot does not use CDeterministicMNList anymore

Also, BuildNewQuorumQuarterMembers uses std::move when possible.

How Has This Been Tested?

invalidate + reconsider 15000 blocks; check logs and perf.

By perf PreComputeQuorumMembers got almost double faster; most improvements came from GetQuorumQuarterMembersBySnapshot as expected.
It should give roughly 2% overall improvement for block validation speed and reindex.

PR:
image

2025-05-20T15:46:53Z [bench]         - m_qblockman: 0.05ms [87.08s]
2025-05-20T15:46:53Z [bench] - Connect block: 21.17ms [249.92s (17.38ms/blk)]

Develop:
image

2025-05-20T16:11:33Z [bench]         - m_qblockman: 0.05ms [93.42s]
2025-05-20T16:11:33Z [bench] - Connect block: 19.06ms [256.95s (17.13ms/blk)]

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone

@knst knst added this to the 23 milestone May 20, 2025
@coderabbitai
Copy link

coderabbitai bot commented May 20, 2025

Walkthrough

The changes remove the quorum scoring and selection methods (CalculateQuorum and CalculateScores) from the CDeterministicMNList class in both its header and implementation files. The quorum member selection logic is reimplemented in llmq/utils.cpp as a set of static helper functions that deterministically compute scores for masternodes based on a cryptographic hash and select the top members for a quorum. Existing functions that previously relied on the removed methods are updated to use the new static helper functions, and some data structures are simplified by replacing specialized masternode lists with standard vectors. No changes are made to other aspects of masternode management or validation.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7ab8647 and 2389d97.

📒 Files selected for processing (1)
  • src/llmq/utils.cpp (8 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/llmq/utils.cpp
⏰ Context from checks skipped due to timeout of 90000ms (9)
  • GitHub Check: linux64_ubsan-build / Build source
  • GitHub Check: linux64-build / Build source
  • GitHub Check: linux64_sqlite-build / Build source
  • GitHub Check: linux64_fuzz-build / Build source
  • GitHub Check: arm-linux-build / Build source
  • GitHub Check: linux64_nowallet-build / Build source
  • GitHub Check: win64-build / Build source
  • GitHub Check: x86_64-apple-darwin / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_multiprocess / Build depends
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/llmq/utils.cpp (2)

168-181: Sorting trick works but is brittle – consider a clearer approach

Using std::sort(scores.rbegin(), scores.rend(), comp) with a comparator that
orders by a.first < b.first gives the desired descending order in the
original container, but the double indirection (reverse-iterators and an
ascending comparator) is non-obvious and easy to break during refactors.

A more idiomatic and self-documenting alternative:

-    std::sort(scores.rbegin(), scores.rend(),
+    std::sort(scores.begin(), scores.end(),
         [](const auto& a, const auto& b) {
-            ...
-            return a.first < b.first;
+            ...
+            return a.first > b.first;  // explicit descending
         });

Same complexity, easier for future maintainers to reason about.


108-730: Code style fails clang-format – please run the formatter

The CI pipeline reports Clang format differences found.
Run clang-format -i src/llmq/utils.cpp (or the repository wrapper script)
and commit the resulting changes to keep the file consistent with the project
style guide.

🧰 Tools
🪛 GitHub Actions: Clang Diff Format Check

[error] 108-730: Clang format differences found. The file does not adhere to the required code style. Please run clang-format to fix formatting issues.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between dc6c35b and 622d488ac669f935e8e1dff07f30ca5574974144.

📒 Files selected for processing (3)
  • src/evo/deterministicmns.cpp (0 hunks)
  • src/evo/deterministicmns.h (0 hunks)
  • src/llmq/utils.cpp (10 hunks)
💤 Files with no reviewable changes (2)
  • src/evo/deterministicmns.cpp
  • src/evo/deterministicmns.h
🧰 Additional context used
🪛 GitHub Actions: Clang Diff Format Check
src/llmq/utils.cpp

[error] 108-730: Clang format differences found. The file does not adhere to the required code style. Please run clang-format to fix formatting issues.

⏰ Context from checks skipped due to timeout of 90000ms (12)
  • GitHub Check: linux64_ubsan-build / Build source
  • GitHub Check: build (x86_64-apple-darwin)
  • GitHub Check: build (powerpc64-linux-gnu)
  • GitHub Check: build (arm64-apple-darwin)
  • GitHub Check: build (riscv64-linux-gnu)
  • GitHub Check: build (x86_64-w64-mingw32)
  • GitHub Check: build (arm-linux-gnueabihf)
  • GitHub Check: build (x86_64-linux-gnu)
  • GitHub Check: build (aarch64-linux-gnu)
  • GitHub Check: x86_64-apple-darwin / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_multiprocess / Build depends
  • GitHub Check: arm-linux-gnueabihf / Build depends
🔇 Additional comments (1)
src/llmq/utils.cpp (1)

694-727: Snapshot size is assumed, not validated

snapshot.activeQuorumMembers[i] is accessed for every element in
sortedAllMns, implicitly assuming that
snapshot.activeQuorumMembers.size() == sortedAllMns.size().
If the snapshot is ever malformed (e.g. from disk corruption or a logic error)
this will trigger OOB reads and UB.

Consider an explicit bounds check or an assert:

assert(snapshot.activeQuorumMembers.size() == sortedAllMns.size());

and gracefully abort if the sizes diverge at runtime on release builds.

@knst knst force-pushed the perf-using-vector branch from 622d488 to 33ef59a Compare May 21, 2025 19:56
@knst knst requested review from PastaPastaPasta and UdjinM6 May 22, 2025 03:54
UdjinM6
UdjinM6 previously approved these changes May 26, 2025
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 33ef59a with one nit

knst and others added 2 commits May 27, 2025 12:35
@knst knst force-pushed the perf-using-vector branch from 7c235c9 to 7ab8647 Compare May 27, 2025 05:36
@knst knst requested a review from UdjinM6 May 27, 2025 09:57
UdjinM6
UdjinM6 previously approved these changes May 27, 2025
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

light ACK 7ab8647

Comment on lines +172 to +173
template <typename List>
static std::vector<CDeterministicMNCPtr> CalculateQuorum(List&& mn_list, const uint256& modifier, size_t maxSize = 0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me better to make this accept a Span so that we have non-templated types

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    auto allMns = dmnman.GetListForBlock(pWorkBlockIndex);
    return CalculateQuorum(allMns, modifier, llmq_params_opt->size, EvoOnly);

allMns has type CDeterministicMNList and there's template to be sure that it works with both types: std::vector<CDeterministicMNCPtr and CDeterministicMNList.

To avoid conversion from CDeterministicMNList to std::vector (which is prety expensive operation) there's used a template param.

I don't see an easy [cheap] way to convert CDeterministicMNList to vector.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, why can CDeterministicMNList be span compatible?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ưhat do you mean?

@knst knst requested a review from PastaPastaPasta May 27, 2025 19:27
…apshot

Co-authored-by: PastaPastaPasta <6443210+PastaPastaPasta@users.noreply.github.com>
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 2389d97

Copy link
Member

@PastaPastaPasta PastaPastaPasta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK 2389d97

@PastaPastaPasta PastaPastaPasta merged commit e3b88cc into dashpay:develop Jun 2, 2025
37 of 38 checks passed
@knst knst deleted the perf-using-vector branch June 16, 2025 10:15
knst pushed a commit to knst/dash that referenced this pull request Sep 15, 2025
… computing rotating quorums

2389d97 refactor: remove un-needed std::move from GetQuorumQuarterMembersBySnapshot (Konstantin Akimov)
7ab8647 perf: move std::vector of masternodes instead copying during rotation quorum calculation (Konstantin Akimov)
026847a refactor: add default argument maxSize for CalcualeQuorum (Konstantin Akimov)
291ea2e refactor: remove useless variable copy (even with move) (Konstantin Akimov)
fd901e0 perf: futher using std::vector instead CDeterministicMNList (Konstantin Akimov)
80faf1b perf: use vector instead DeterministicMNList for calculation of rotating quorums (Konstantin Akimov)
77273d5 perf: use std::move during quorum calculation (Konstantin Akimov)

Pull request description:

  ## Issue being fixed or feature implemented
  Helper to get quorum members uses CDeterministicMNList as a source of data.
  It makes temporary calculation slower, because temporary lists of masternodes should not construct heavy objects CDeterministicMNList just to keep a list of them.

  ## What was done?
   - helpers `CalculateQuorum`, `CalculateScores` are moved from class `CDeterministicMNList` to `llmq::utils` so far as they don't actually use any private data of `CDeterministicMNList`
   - helper `CalculateQuorum` can accept as source of data not only CDeterministicMNList but std::vector with shared ptrs.
   - helper `BuildNewQuorumQuarterMembers` does not use CDeterministicMNList for `MnsNotUsedAtH`
   - `GetMNUsageBySnapshot` does not use CDeterministicMNList anymore

  Also, `BuildNewQuorumQuarterMembers` uses `std::move` when possible.

  ## How Has This Been Tested?
  invalidate + reconsider 15000 blocks; check logs and `perf`.

  By perf `PreComputeQuorumMembers` got almost double faster; most improvements came from `GetQuorumQuarterMembersBySnapshot` as expected.
  It should give roughly 2% overall improvement for block validation speed and reindex.

  PR:
  <img width="747" alt="image" src="https://github.com/user-attachments/assets/d27778fa-cc09-4bd7-bccd-601fa6df75f3" />
  ```
  2025-05-20T15:46:53Z [bench]         - m_qblockman: 0.05ms [87.08s]
  2025-05-20T15:46:53Z [bench] - Connect block: 21.17ms [249.92s (17.38ms/blk)]
  ```

  Develop:
  <img width="747" alt="image" src="https://github.com/user-attachments/assets/aff54059-703d-4c76-967a-15279b1b420c" />
  ```
  2025-05-20T16:11:33Z [bench]         - m_qblockman: 0.05ms [93.42s]
  2025-05-20T16:11:33Z [bench] - Connect block: 19.06ms [256.95s (17.13ms/blk)]
  ```

  ## Breaking Changes
  N/A

  ## Checklist:
  - [x] I have performed a self-review of my own code
  - [ ] I have commented my code, particularly in hard-to-understand areas
  - [ ] I have added or updated relevant unit/integration/functional/e2e tests
  - [ ] I have made corresponding changes to the documentation
  - [x] I have assigned this pull request to a milestone

ACKs for top commit:
  UdjinM6:
    utACK 2389d97
  PastaPastaPasta:
    utACK 2389d97

Tree-SHA512: 86db57e74eb815cc030421610d20e2dd90dfd18fcf429f97ba96681b69539cce54b0f4c9146e29beb541d1e0193919a1242bd08cba8df7017cfe4b42b8be7a63
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants